<template>
	<view class="box">
		<view class="top">
			<u-navbar :is-back="true" title="我的收藏" :border-bottom="false"></u-navbar>
		</view>
		<view class="collect " v-if="collect.length>0">
			<u-swipe-action :show="item.show" :index="index" v-for="(item, index) in collect" :key="item.id" @click="click"
				@open="open" :options="options" >
				<view class="item container_br jsbc">
					<view class="left">
						<image :src="item.goods.images_format[0]" mode="widthFix" style="width: 150rpx;"></image>
					</view>
					<view class="right">
						<view class="name">
							{{item.goods.name}}
						</view>
						<view class="price" style="color: red;">
							￥{{item.goods.min_price}}
						</view>
						<view class="time">
							收藏时间：{{item.updated_at}}
						</view>
					</view>
				</view>
			</u-swipe-action>
		</view>
		<view class="noData" style="width: 410rpx; height: 240rpx ;margin: 40rpx auto;" v-else>
			<image src="../../static/img/empty.png" mode="widthFix" style="width: 410rpx; height: 240rpx;"></image>
		</view>
	</view>
</template>

<script>
	// import {
	// 	getMycollect
	// } from '@/api/user.js'

	import {
		getCollect,
		cancelCollect
	} from '@/api/index.js'
	import {
		closeload,
		toast, toastload
	} from '@/utils/common.js'
	import baseUrl from '@/config/config.js'
	export default {
		data() {
			return {
				user_id: "",
				collect: [],
				options: [{
					text: '删除',
					style: {
						backgroundColor: '#dd524d'
					}
				}]
			};
		},
		onLoad() {
			this.user_id = uni.getStorageSync("user_id") || ""
			this.init()
		},
		methods: {
			init() {
				toastload()
				getCollect().then(res => {
					console.log("我的收藏", res);
					closeload()
					this.collect = res.list

				})
			},
			cancelBtn(id) {
				cancelCollect({
					goods_id: id
				}).then(res => {
					if (res.code == 200) {
						toast("取消收藏成功")
					}
				})
			},
			click(index, index1) {
				if (index1 == 0) {
					this.cancelBtn(this.collect[index].goods_id)
					this.collect.splice(index, 1);
				}
			},
			// 如果打开一个的时候，不需要关闭其他，则无需实现本方法
			open(index) {
				// 先将正在被操作的swipeAction标记为打开状态，否则由于props的特性限制，
				// 原本为'false'，再次设置为'false'会无效
				this.collect[index].show = true;
				this.collect.map((val, idx) => {
					if (index != idx) this.collect[idx].show = false;
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.collect{
		width: 710rpx;
		margin: 20rpx auto;
		box-sizing: border-box;
		padding: 1rpx;
	}
	.item{
		.left{
			width: 150rpx;
			height: 150rpx;
			overflow: hidden;
			border-radius: 20rpx;
		}
		.right{
			width: 480rpx;
			height: 150rpx;
			box-sizing: border-box;
			padding: 10rpx 0;
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			.time{
				font-size: 26rpx;
				color: #666;
			}
		}
	}

</style>